/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software Foundation,
 *    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package JET.physics;

import org.w3c.dom.*;

/**
 *
 * @author 057Ry
 */
public class MaterialPair
{
    public double friction;
    public double elasticity;
    public boolean collide;

    public MaterialPair()
    {
        friction = 0.9;
        elasticity = 0.8;
        collide = true;
    }

    public MaterialPair(double f, double e, boolean c)
    {
        friction = f;
        elasticity = e;
        collide = c;
    }

    public MaterialPair(Element materialPairXML)
    {
        NodeList nodeList = materialPairXML.getChildNodes();
        
        for(int i=0; i<nodeList.getLength(); i++)
        {
            Node node = nodeList.item(i);
            if (node instanceof Element)
            {
                Element element = (Element)nodeList.item(i);

                if (element.getTagName().equals("bounce"))
                {
                    Text textNode = (Text) element.getFirstChild();
                    String text = textNode.getData().trim();
                    this.elasticity = Double.parseDouble(text);
                }
                else if (element.getTagName().equals("friction"))
                {
                    Text textNode = (Text) element.getFirstChild();
                    String text = textNode.getData().trim();
                    this.friction = Double.parseDouble(text);
                }
                else if (element.getTagName().equals("collide"))
                {
                    Text textNode = (Text) element.getFirstChild();
                    String text = textNode.getData().trim();
                    this.collide = Boolean.parseBoolean(text);
                }
            }
        }
    }

    public Element getXMLRepresentation(Document doc)
    {
        Element rootElement = doc.createElement("materialPair");

        rootElement.appendChild(doc.createTextNode("\n"));

        Element bounceEl = doc.createElement("bounce");
            bounceEl.appendChild( doc.createTextNode(""+elasticity) );

        Element frictionEl = doc.createElement("friction");
            frictionEl.appendChild( doc.createTextNode(""+friction) );

        Element collideEl = doc.createElement("collide");
            collideEl.appendChild( doc.createTextNode(""+collide) );

        rootElement.appendChild(bounceEl);
        rootElement.appendChild(frictionEl);
        rootElement.appendChild(collideEl);

        return rootElement;
    }
}
